package com.preloader
{
    import mx.containers.Canvas;
    import mx.controls.Label;
    import flash.display.Loader;
    import mx.events.FlexEvent;
    import flash.events.Event;
    import flash.display.Bitmap;
    import flash.utils.ByteArray;
    import flash.display.BitmapData;
    import flash.display.MovieClip;
    import mx.utils.GraphicsUtil;
    import mx.graphics.codec.PNGEncoder;
    import flash.display.Sprite;
    import flash.display.Graphics;
    import mx.utils.ColorUtil;
    import flash.display.GradientType;
    import flash.display.SpreadMethod;
    import flash.geom.*;
    import mx.messaging.SubscriptionInfo;
    import mx.controls.TextInput;
    import mx.core.FlexSprite;

    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

    public class ProgressBar extends Loader
    {
        // Logo picture
        [Embed(source="../../assets/images/logo.gif")]
        [Bindable]
        private var logoClass: Class;
        private var Logo: Bitmap;

        private var m_Ready: Boolean;
        private var m_Progress: Number;
        private var m_BitmapData: BitmapData;

        private var txt : TextField;

        // Constant to change to fit your own layout
        private static var ProgressWidth: int = 400; // Progress bar width
        private static var ProgressHeight: int = 8; // Progress bar height
        private static var PictureWidth: int = 400;  // Logo picture width
        private static var PictureHeight: int = 374; // Logo picture height
        private static var LeftMargin: int = 1;      // Left Margin
        private static var RightMargin: int = 2;     // Right Margin
        private static var Spacing: int = 10;        // Spacing between logo and progress bar
        private static var TopMargin: int = 1;       // Top Margin
        private static var BottomMargin: int = 1;    // Bottom Margin


        private static var ProgressBarBackground: uint = 0x7795C7;
        private static var progressBarOuterBorder: uint = 0x000000;
        private static var ProgressBarColor: uint = 0x274577;
        private static var ProgressBarInnerColor: uint = 0xFFFFFF;

        private static var TextFormatSize: int = 14;
        private static var TextFormatFont: String = "Verdana";
        private static var TextFormatColor: uint = 0x274577;

        private static var TextFieldText: String = "Cargando... ";


        public function ProgressBar(): void{
            super();

            // Creates the textField
            txt = new TextField();
            txt.x = LeftMargin;
            txt.y = getHeight() - 30;
            txt.width = PictureWidth;
            txt.height = 50;
            txt.autoSize = TextFieldAutoSize.CENTER;

			// Add some format to the text that's going to be displayed
			var format:TextFormat = new TextFormat();
            format.font = TextFormatFont;
            format.color = TextFormatColor;
            format.size = TextFormatSize;
            format.bold = true;
            txt.defaultTextFormat = format;

            // Add the text, so it takes the format changes
            txt.appendText(TextFieldText);

            m_Progress = 0;
            Logo = new logoClass as Bitmap;
            this.addEventListener(Event.RENDER, renderEventHandler);
        }
        private function renderEventHandler(event: Event): void{

        }
        public function refreshProgressBar(): void{
            m_BitmapData = drawProgress(); // Create the bitmapdata object
            var encoder: PNGEncoder = new PNGEncoder();
            var byteArray: ByteArray = encoder.encode(m_BitmapData); // Encode the bitmapdata to a bytearray
            this.loadBytes(byteArray); // Draw the bitmap on the loader object
        }
        public function getWidth(): Number{
            return LeftMargin + PictureWidth + RightMargin;
        }
        public function getHeight(): Number{
            return TopMargin + PictureHeight + BottomMargin;
        }
        private function drawProgress(): BitmapData{
            // Create the bitmap class object
            var bitmapData: BitmapData = new BitmapData(getWidth(), getHeight(), true, 0);

            //Draw the Logo
            bitmapData.draw(Logo.bitmapData, null, null, null, null, true);

            // Draw the Progress Bar
            var sprite: Sprite = new Sprite();
            var graph: Graphics = sprite.graphics;

            // Draw the progress bar background
            graph.beginFill(ProgressBarBackground);
        	    //graph.lineStyle(1, progressBarOuterBorder, 1, true);
            var containerWidth: Number = ProgressWidth;
            var px: int = 5;
            var py: int = PictureHeight - 40;
            graph.drawRoundRect(px, py, containerWidth-10, ProgressHeight, 5);
            containerWidth -= 5;
            var progressWidth: Number = containerWidth * m_Progress / 100;
            var matr:Matrix = new Matrix();
			matr.createGradientBox(10, 50, 0, 0, 0);
            graph.beginGradientFill(GradientType.RADIAL,[ProgressBarBackground,ProgressBarColor],[100,100],[0,33],matr,SpreadMethod.REFLECT);
				//graph.beginFill(ProgressBarColor);
    	        //graph.lineStyle(1, ProgressBarInnerColor, 1, true);
            graph.drawRoundRect(px+2, py+1 , progressWidth - 7, ProgressHeight-2,5);

			// Change the text of the progress textField
			txt.replaceText(TextFieldText.length,txt.length,progress.toFixed(0)+"%");
			// Add the progress textField
			sprite.addChild(txt);

            bitmapData.draw(sprite);

            return bitmapData;
        }
        public function set ready(value: Boolean): void{
            m_Ready = value;
            this.visible = !value;
        }
        public function get ready(): Boolean{
            return m_Ready;
        }
        public function set progress(value: Number): void{
            m_Progress = value;
        }
        public function get progress(): Number{
            return m_Progress;
        }
    }
}